package com.qdxwx.manage.support;

import com.qdxwx.common.utils.TKServlet;
import com.qdxwx.models.manager.Manager;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;

import java.util.Set;

/**
 * Session 工具类
 * 
 * @author qdxwx
 */
public class SessionUtils {


    public static final String SESSION_MANAGER_NAME = "login_manager";
    public static final String SESSION_MANAGER_ROLES_NAME = "login_manager_roles";
    public static final String SESSION_MANAGER_PERMS_NAME = "login_manager_perms";

    public static Manager getManager() {
        return getManager(TKServlet.getRequest());
    }

    public static Manager getManager(HttpServletRequest request) {
        try {
            return (Manager) request.getSession().getAttribute(SESSION_MANAGER_NAME);
        } catch (Exception ex) {
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    public static Set<String> getPermissions(HttpServletRequest request) {
        try {
            return (Set<String>) request.getSession().getAttribute(SESSION_MANAGER_PERMS_NAME);
        } catch (Exception ex) {
            return null;
        }
    }

    @SuppressWarnings("unchecked")
    public static Set<String> getRoles(HttpServletRequest request) {
        try {
            return (Set<String>) request.getSession().getAttribute(SESSION_MANAGER_ROLES_NAME);
        } catch (Exception ex) {
            return null;
        }
    }

    public static boolean isAdmin(HttpServletRequest request) {
        //获取当前用户权限
        Set<String> roles = getRoles(request);
        if (roles == null || roles.size() < 1) {
            return false;
        }
        return roles.contains("supper") || roles.contains("admin");
    }

    public static void setManager(Manager manager) {
        HttpSession session = TKServlet.getSession();
        session.setAttribute(SESSION_MANAGER_NAME, manager);
    }

    public static void setSession(HttpServletRequest request, Manager manager, Set<String> roles, Set<String> perms) {
        HttpSession session = request.getSession();
        session.setAttribute(SESSION_MANAGER_NAME, manager);
        session.setAttribute(SESSION_MANAGER_ROLES_NAME, roles);
        session.setAttribute(SESSION_MANAGER_PERMS_NAME, perms);
        session.setMaxInactiveInterval(3600);
    }

    public static void clearSession(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute(SESSION_MANAGER_NAME);
        session.removeAttribute(SESSION_MANAGER_ROLES_NAME);
        session.removeAttribute(SESSION_MANAGER_PERMS_NAME);
        session.invalidate();
    }

    public static long getUserId() {
        Manager manager = getManager();
        if (manager != null) {
            return manager.getUserId();
        }
        return 0;
    }

    public static int getManagerId() {
        Manager manager = getManager();
        if (null != manager) {
            return manager.getManagerId();
        }
        return 0;
    }

    public static String getLoginName() {
        Manager manager = getManager();
        if (null != manager) {
            return manager.getLoginName();
        }
        return "";
    }
}
